package com.sky.service.impl;


import com.fasterxml.jackson.databind.ser.Serializers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    private final CategoryMapper categoryMapper;

    private final DishMapper dishMapper;

    private final SetmealMapper setmealMapper;

    @Override
    public PageResult query(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        Page<Category> page = categoryMapper.query(categoryPageQueryDTO);
        long total = page.getTotal();
        List<Category> records = page.getResult();
        return new PageResult(total,records);
    }

    @Override
    public void save(CategoryDTO categoryDTO) {
        //补全相关属性
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        category.setStatus(StatusConstant.ENABLE);
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.insert(category);
    }

    @Override
    public void setOrStop(Integer status, Long id) {
        categoryMapper.setOrStop(status,id);
    }

    @Override
    public void updateSort(CategoryDTO categoryDTO) {
        Category category = new Category();
        category.setUpdateTime(LocalDateTime.now());
        category.setId(BaseContext.getCurrentId());
        categoryMapper.updateSort(category);
    }

    @Override
    public void deleteById(Long id) {
        //判断分类下面有没有菜品和套餐，没有才可以删除
        int Dishcount = dishMapper.countById(id);
        int Setmealcount = setmealMapper.countById(id);
       log.info("菜品所属为：{},{}",Dishcount,Setmealcount);
        if(Dishcount == 0 && Setmealcount == 0){
            categoryMapper.deleteById(id);
        }else{
            //输出错误信息到页面
            throw new RuntimeException("当前分类下有菜品或套餐，不能删除");
        }


    }

    @Override
    public List<Category> queryByType(Integer type) {
        Category category = new Category();
        category.setType(type);
        category.setStatus(StatusConstant.ENABLE); // 只查询启用状态的分类
        return categoryMapper.queryByType(category);
    }
}
